package com.javaenzk.wfx.manager.controller;
import com.javaenzk.wfx.manager.po.Tbshoppingcar;
import com.javaenzk.wfx.manager.po.Wxb_Good;
import com.javaenzk.wfx.manager.service.IGoodsService;
import com.javaenzk.wfx.manager.util.ResultJson;
import com.javaenzk.wfx.manager.vo.JsonResultVO;
import com.javaenzk.wfx.manager.vo.MerchantInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Resource
    private IGoodsService goodsService;

    @Autowired
    private RestTemplate restTemplate;

    private Logger logger = Logger.getLogger(GoodsController.class);

    /**
     * 查询商品列表
     * @param
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Wxb_Good> queryGoodsList(){
        List<Wxb_Good> goodList = goodsService.queryGoodsList();
        return goodList;
    }

    /**
     * 通过goodsId查询套餐列表
     * @param goodsId
     * @return
     */
    @RequestMapping("/details")
    public String queryGoodsDetailsByGoodsId(Model model,String goodsId){
        Wxb_Good wxbGood = goodsService.queryGoodsById(goodsId);
        model.addAttribute("goods",wxbGood);
        return "goods_details";
    }

    @HystrixCommand(fallbackMethod = "addShoppingbeitai")
    @RequestMapping("/addshopping")
    @ResponseBody
    public JsonResultVO addShopping(String goodsId, String sku){
        logger.info("addShopping");
        Tbshoppingcar tbShoppingCar = new Tbshoppingcar();
        tbShoppingCar.setShpgoodsid(goodsId);

        tbShoppingCar.setShpgoodsnum(1);
        tbShoppingCar.setShpgoodssku(sku);
        tbShoppingCar.setShpuserid("03452414");
        tbShoppingCar.setShpmerchantid("4464ba09-eee5-46ad-88dd-319c721125b5");

        JsonResultVO jsonResultVO  = restTemplate.postForObject("http://wfx-cart1/add",tbShoppingCar,JsonResultVO.class);
        return jsonResultVO;

    }


    public JsonResultVO addShoppingbeitai(String goodsId, String sku){

        logger.info("addShoppingbeitai触发熔断器了22");
        JsonResultVO jsonResultVO = new JsonResultVO();
        jsonResultVO.setCode(0);
        jsonResultVO.setMsg("触发熔断器了22");
        return jsonResultVO;
    }


    @RequestMapping("/shoppingList")
    public String queryShoppingList(Model model){

        MerchantInfo[] infos = restTemplate.getForObject("http://wfx-cart1/list", MerchantInfo[].class);
        List<MerchantInfo> list = Arrays.asList(infos);
        model.addAttribute("list",list);
        return "shoppingcart";
    }




}
